<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" ref="queryForm" v-show="showSearch" label-width="88px">
                <el-form-item :label="$t('inventoryManagement.supplierBatchNo')" prop="lichn">
                    <el-input v-model="queryParams.lichn" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.supplierBatchNo')})" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.batch')" prop="lot">
                    <el-input v-model="queryParams.lot" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.batch')})" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.materialName')" prop="materialName">
                    <el-input v-model="queryParams.materialName" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.materialName')})" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.materialNo')" prop="materialCode">
                    <el-input v-model="queryParams.materialCode" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.materialNo')})" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.supplierName')" prop="supplierName">
                    <el-input v-model="queryParams.supplierName" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.supplierName')})" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.supplierNo')" prop="supplierCode">
                    <el-input v-model="queryParams.supplierCode" :placeholder="$t('public.placeholder',{placeholder: $t('inventoryManagement.supplierNo')})" clearable size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.useDecision')" prop="usingDecision">
                    <el-select size="small" v-model="queryParams.usingDecision" :placeholder="$t('public.select_holder',{select_holder: $t('inventoryManagement.useDecision')})">
                        <el-option :label="$t('Toberead.all')" value="" />
                        <el-option v-for="dict in dict.type.NYIQC_NCR_DECISION" :key="dict.label" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('inventoryManagement.approvalStatus')" prop="auditResult">
                    <el-select v-model="queryParams.auditResult" :placeholder="$t('public.select_holder',{select_holder: $t('ncr.reviewStatus')})" style="width:100%;" clearable size="small">
                        <el-option :label="$t('Toberead.all')" value="" />
                        <el-option v-for="dict in dict.type.NYIQC_AUDIT_RESULT" :key="dict.label" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <div style="display:flex;justify-content: center;">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.search')}}</el-button>
                <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
            </div>
        </div>
        <div class="app-container">
            <div class="container-header">
                <div class="header-left">
                    {{$t('inventoryManagement.reviewUnqualified')}}
                </div>
            </div>
            <el-row :gutter="10" class="mb8 btnGroup">
                <el-col :span="1.5">
                    <el-button size="mini" class="exportitemBtn" @click="handleExport" v-hasPermi="['ny-iqc:unqualified:export']"><img src="@/assets/images/icons/daochu.png" alt=""> {{$t('public.export')}}</el-button>
                </el-col>
                <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
            </el-row>

            <div class="container-table">
                <el-table :height="tableHeight" border :row-class-name="tableRowClassName" v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column :label="$t('inventoryManagement.supplierBatchNo')" align="left" prop="lichn" minWidth="160" />
                    <el-table-column :label="$t('inventoryManagement.batch')" minWidth="160" align="left" prop="lot" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.materialNo')" minWidth="160" align="left" prop="materialCode" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.materialName')" minWidth="200" align="left" prop="materialName" />
                    <el-table-column :label="$t('inventoryManagement.materialDocument')" minWidth="160" align="left" prop="mblnr" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.edition')" align="left" prop="version" :show-overflow-tooltip="true" />

                    <el-table-column :label="$t('inventoryManagement.incomingQuantity')" align="left" prop="batchAmount" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.unit')" align="center" prop="mengeneinh" minWidth="180" />
                    <el-table-column :label="$t('inventoryManagement.supplierName')" minWidth="160" align="left" prop="supplierName" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.supplierNo')" align="left" prop="supplierCode" minWidth="120" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.purchaser')" align="left" prop="supplierCode" minWidth="120" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.purchaseDocument')" align="left" prop="ebeln" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.inspectionStartTime')" align="left" prop="inspectionStartTime" minWidth="140" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.inspectionEndTime')" align="left" prop="inspectionEndTime" minWidth="160" :show-overflow-tooltip="true" />
                    <el-table-column :label="$t('inventoryManagement.useDecision')" align="left" prop="usingDecision" minWidth="140" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{usingDecisionFilter(scope.row)}}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('ncr.reviewStatus')" align="left" prop="auditResult" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            {{auditResultFilter(scope.row)}}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('public.opera')" fixed="right" align="center" width="100">
                        <template slot-scope="scope">
                            <div class="action-wrap">
                                <table-body-btn type="view" :name="$t('home.view')" @click.native="handleView(scope.row)" v-hasPermi="['ny-iqc:unqualified:getInfo']" />
                            </div>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
            </div>

        </div>
    </list-layout>
</template>

<script>
import {listUnqualified,} from '@/api/inventoryManagement/inspectedInventory'
import FileUploadInput from '@/components/FileUploadInput'
import {mapState} from 'vuex'

export default {
    name: 'inspectionExemption',
    dicts: [
        'sys_normal_disable',
        'NYIQC_NCR_DECISION',
        'NYIQC_AUDIT_RESULT',
    ],
    components: {
        FileUploadInput,
    },
    computed: {
        ...mapState({
            isFold: (state) => state.app.isFold,
        }),
    },
    watch: {
        isFold(val) {
            if (val === true) {
                this.tableHeight = 'calc(100vh - 308px)'
            } else {
                this.tableHeight = 'calc(100vh - 446px)'
            }
        },
    },
    data() {
        return {
            direction: 'rtl',
            addInspectionExemption: false,
            tableHeight: 'calc(100vh - 308px)',
            drawer: false,
            approvalOpen: false,
            viewInspectionExemption: false,
            addModel: {},
            viewModel: {
                file: 'xxx.jpeg',
            },
            editModel: {},
            approvalForm: {
                status: '1',
                textarea: '',
            },
            selBtn: 1,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                lichn: '',
                lot: '',
                materialName: '',
                materialCode: '',
                supplierName: '',
                supplierCode: '',
                auditResult: '',
                usingDecision: '',
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                materialName: [
                    {
                        required: true,
                        message: this.$t('formValidation.materialNameCannotBeEmpty'),
                        trigger: 'blur',
                    },
                ],
                materialNo: [
                    {
                        required: true,
                        message: '物料编号不能为空',
                        trigger: 'blur',
                    },
                ],
                supplierNo: [
                    {
                        required: true,
                        message: '供应商编号不能为空',
                        trigger: 'blur',
                    },
                ],
            },
            fullscreen: false,
        }
    },
    created() {
        this.getList()
    },
    methods: {
        usingDecisionFilter(row) {
            if (row.usingDecision) {
                let io = this.dict.type.NYIQC_NCR_DECISION.find((item) => {
                    return item.value == row.usingDecision
                })
                return io.label
            } else return ''
        },
        auditResultFilter(row) {
            if (row.auditResult) {
                let io = this.dict.type.NYIQC_AUDIT_RESULT.find((item) => {
                    return item.value == row.auditResult
                })
                return io.label
            } else return ''
        },
        effectDateChange(e, scope) {
            if (parseInt(e) > 0) {
                this.typeList[scope.$index].endDate = this.getNewDay(
                    scope.row.approvalTime,
                    e
                )
            }
        },
        getNewDay(dateTemp, days) {
            var dateTemp = dateTemp.split('-')
            //转换为MM-DD-YYYY格式
            var nDate = new Date(
                dateTemp[1] + '-' + dateTemp[2] + '-' + dateTemp[0]
            )
            var millSeconds = Math.abs(nDate) + days * 24 * 60 * 60 * 1000
            var rDate = new Date(millSeconds)
            var year = rDate.getFullYear()
            var month = rDate.getMonth() + 1
            if (month < 10) month = '0' + month
            var date = rDate.getDate()
            if (date < 10) date = '0' + date
            return year + '-' + month + '-' + date
        },
        fileChange(e) {
            console.log(e)
        },
        drawerClose() {
            this.drawer = false
        },
        /** 查询字典类型列表 */
        getList() {
            this.loading = true
            listUnqualified(this.queryParams).then((response) => {
                if (response.code == 200 && response.rows) {
                    this.typeList = response.rows
                    this.total = response.total
                    this.loading = false
                }
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                dictId: undefined,
                dictName: undefined,
                dictType: undefined,
                status: '0',
                remark: undefined,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            if (this.ids.length === 0) {
                this.$modal.msgWarning('请至少选择一条额外检验单!')
                return
            }
            this.$router.push({ path: '/additionalInspection/add/index/' + 0 })
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.typeList.forEach((io, index) => {
                console.log('includes', this.ids.includes(io.id))
                if (this.ids.includes(io.id)) {
                    io.isHighLight = true
                } else {
                    io.isHighLight = false
                }
            })
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        tableRowClassName({ row, rowIndex }) {
            console.log('rowIndex', rowIndex, row)
            if (row.isHighLight === true) {
                console.log('rowIndex', row)
                return 'rowHighLight' //若isHighLight为true，则使用“rowHighLight”样式
            } else {
                return ''
            }
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const dictId = row.dictId || this.ids
            getType(dictId).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改字典类型'
            })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['addForm'].validate((valid) => {
                if (valid) {
                    this.$modal.msgSuccess('提交成功!')
                    this.addInspectionExemption = false
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        /** 发起检验提交 */
        submitAddForm: function () {
            this.$modal.msgSuccess('发起成功!')
            this.addInspection = false
        },

        /** 删除按钮操作 */
        handleDelete(row) {
            if (this.ids.length === 0) {
                this.$modal.msgWarning('请至少选择一条额外检验单!')
                return
            }
            this.$modal.msgSuccess(this.$t('processDefinition.successfullyDeleted'))
        },
        /** 查看按钮操作 */
        handleView(row) {
            this.$router.push({
                path: '/reviewUnqualified/detail/index/' + row.id,
            })

            //   if(row.hasRead == 0){
            //     readNotice(row.noticeId).then(res=>{
            //       if(res.code == 200){
            //         this.handleQuery()
            //       }
            //     })
            //   }
        },
        /** 编辑按钮操作 */
        handleEdit(row) {
            this.drawer = true
        },
        /** 关闭抽屉操作 */
        handleClose(row) {
            this.drawer = false
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'ny-iqc/unqualified/export',
                {
                    ...this.queryParams,
                },
                `type_${new Date().getTime()}.xlsx`
            )
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
            refreshCache().then(() => {
                this.$modal.msgSuccess('刷新成功')
            })
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    float: left;
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    line-height: 40px;
    display: flex;
    justify-content: center;
    border-bottom: 2px solid #0b7be3;
}
.header-right {
    float: right;
}
.container-header {
    float: left;
    width: 100%;
    border-bottom: 1px solid #ededf2;
}
.container-table {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 100%;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}
::v-deep .rowHighLight {
    background: oldlace;
}
</style>
